<?php
include './meihe/common.php';

$act = isset($_GET['act']) ? daddslashes($_GET['act']) : null;

switch ($act) {
	case 'meihe':
		$result = array('code' => 0, 'yxts' => $yxts, 'count1' => $count1, 'count2' => $count2, 'meihefh' => $meihefh, 'jrkthy' => $jrkthy, 'scjr' => $scjr, 'vipyum' => $vipyum, 'duanljk' => $duanljk, 'fwjr' => $fwjr, 'ymzs' => $ymzs, 'fwjl' => $fwjl, 'sczs' => $sczs, 'hbmd' => $hbmd, 'hmd' => $hmd, 'count3' => $count3, 'count4' => $count4, 'count5' => round($count5, 2), 'count6' => $count6, 'fjyum' => $fjyum, 'count7' => $count7, 'count8' => round($count8, 2), 'count9' => round($count9, 2), 'count10' => round($count10, 2), 'count11' => round($count11, 2), 'count12' => round($count12, 2), 'count13' => round($count13, 2), 'count14' => round($count14, 2), 'chart' => getDatePoint());
		exit(json_encode($result));
		break;

	case 'dlapi':
		$hh_duan = mysqli_fetch_all($DB->query('SELECT name,sbm FROM meihe_api where jktz=1 '));
		echo json_encode(array('data' => $hh_duan));
		break;

	case 'cxdqsj':
		$url = addslashes($_POST['url']);

		if (!$url) {
			$result = array('code' => -1, 'msg' => '请输入查询的域名！');
			exit(json_encode($result));
			break;
		}

		if (strexists($url, 'http://')) {
			$result = array('code' => -1, 'msg' => '域名无需输入http://');
			exit(json_encode($result));
			break;
		}

		if (!strexists($url, '.')) {
			$result = array('code' => -1, 'msg' => '域名格式疑似不正确');
			exit(json_encode($result));
			break;
		}

		$row = $DB->get_row("SELECT * FROM meihe_list WHERE domain='{$url}' limit 1");

		if (!$row) {
			$result = array('code' => -1, 'msg' => '查询域名：' . $url . '</br>查询结果：<font color="red">还未开通会员！</font>');
			exit(json_encode($result));
			break;
		}

		$active = $row['type'] == 1 ? '<font color="green">激活(可以正常使用)</font>' : '<font color="red">封禁(无法正常使用)</font>';
		$fhy = $row['fhy'] == 1 ? '<font color="red">是</font>' : '<font color="green">否</font>';
		$result = array('code' => 0, 'msg' => '查询域名：' . $url . '</br>查询结果：<font color="green">正版会员</font></br>会员状态：' . $active . '</br>是否泛会员：' . $fhy . '</br>开通时间：' . $row['date'] . '</br>到期时间：' . $row['endtime']);
		exit(json_encode($result));
		break;

	case 'image_workorder':
		$id = intval($_GET['id']);
		$row = $DB->get_row("SELECT * FROM meihe_workorder WHERE id='{$id}' limit 1");

		if (!$row) {
			exit('{"code":-1,"msg":"该工单记录不存在！"}');
			break;
		}

		$picurl = explode(',', $row['picurl']);
		$i = 1;
		$ii = 1;
		$data = array();
		foreach ($picurl as $image) {
			$data[] = array('alt' => '工单ID：' . $row['id'] . '图' . $i++, 'pid' => $ii++, 'src' => $image, 'thumb' => $image);
		}

		$result = array('title' => '工单ID：' . $row['id'], 'id' => $id, 'start' => 0, 'data' => $data);
		exit(json_encode($result));
		break;

	case 'hyapi':
		$dq = isset($_GET['dq']) ? $_GET['dq'] : $_POST['dq'];
		$endtime = daddslashes($_POST['endtime']);
		$domain = daddslashes($_GET['domain'] ?? '');
		$qq = daddslashes($_GET['qq']);
		$type = daddslashes($_POST['type']);
		$syskey = isset($_GET['key']) ? $_GET['key'] : $_POST['key'];

		if (!$syskey) {
			exit('{"code":-1,"msg":"接口密钥不能为空"}');
			break;
		}

		if ($syskey !== $conf['hh2_key']) {
			exit('{"code":-1,"msg":"接口密钥不正确"}');
			break;
		}

		if (!$dq) {
			exit('{"code":-1,"msg":"开通时间不能为空"}');
			break;
		}

		$row = $DB->get_row("SELECT * FROM meihe_list WHERE domain='{$domain}' limit 1");
		$hmd = $DB->get_row("SELECT * FROM meihe_hbmd WHERE domain='{$domain}' limit 1");

		if ($row) {
			exit('{"code":2,"msg":"当前域名已存在无法重复开通！"}');
			break;
		}

		if ($hmd && $hmd['type'] == 2) {
			exit('当前域名为黑名单，无法开通VIP会员！');
			break;
		}

		if ($dq == 1) {
			$endtime = date('Y-m-d', strtotime('+ 1 months'));
		} elseif ($dq == 0) {
			$endtime = date('Y-m-d', strtotime('+ 1 days'));
		} elseif ($dq == 63) {
			$ffktrq = 0 > 0 ? 0 : 12;
			$endtime = date('Y-m-d H:i:s', strtotime("+{$ffktrq} months", time()));
		} elseif ($dq == 12) {
			$endtime = date('Y-m-d', strtotime('+ 1 years'));
		} elseif ($dq == 3) {
			$endtime = date('Y-m-d', strtotime('+ 3 months'));
		} elseif ($dq == 5) {
			$endtime = date('Y-m-d', strtotime('+ 5 months'));
		}

		if (strlen($qq) > 12 || !is_numeric($qq) || strlen($qq) < 5 || !preg_match('/^[0-9]+$/', $qq)) {
			exit('{"code":2,"msg":"请输入正确的QQ号！"}');
			break;
		}

		$sql = 'insert into `meihe_list` (`domain`,`date`,`type`,`endtime`,`qq`,`fhy`) values (\'' . $domain . '\',\'' . $date . '\',\'1\',\'' . $endtime . '\',\'' . $qq . '\',\'' . $conf['fhyyum'] . '\')';
		if (!$DB->query($sql)) {
			exit('{"code":-1,"msg":"开通VIP失败！' . $DB->{'error'}() . '"}');
			break;
		}
		exit('{"code":1,"msg":"恭喜您,开通会员成功！"}');
		break;

	case 'bmdapi':
		$domain = isset($_POST['domain']) ? daddslashes($_POST['domain']) : $_GET['domain'];
		$type = daddslashes($_POST['type']);
		$syskey = isset($_GET['key']) ? $_GET['key'] : $_POST['key'];

		if (!$syskey) {
			exit('{"code":-1,"msg":"接口密钥不能为空"}');
			break;
		}

		if ($syskey !== $conf['hh4_key']) {
			exit('{"code":-1,"msg":"接口密钥不正确"}');
			break;
		}

		$row = $DB->get_row("SELECT * FROM meihe_hbmd WHERE domain='{$domain}' limit 1");

		if ($row && $row['type'] == 3) {
			exit('{"code":2,"msg":"当前域名已存在无法重复添加！"}');
			break;
		}

		if ($row && $row['type'] == 2) {
			exit('当前域名为黑名单！');
			break;
		}

		if (strexists($domain, '.')) {
			$DB->query('insert into `meihe_hbmd` (`domain`,`date`,`type`,`fwcsxx`,`bmdgg`,`hmdyy`) values (\'' . $domain . '\',\'' . $date . '\',\'3\',\'' . $conf['bxfwcs'] . '\',\'' . $conf['bmdgbgg'] . '\',\'' . $hmdyy . '\')');
			exit('{"code":1,"msg":"恭喜您,添加白名单成功！"}');
			break;
		}
		exit('{"code":2,"msg":"请您输入正确的域名！"}');
		break;

	case 'vippay':
		$domain = daddslashes($_POST['domain']);
		$qq = daddslashes($_POST['qq']);
		$ktrq = intval($_POST['ktrq']);

		$vipym2 = $DB->get_row("SELECT * FROM meihe_list WHERE domain='{$domain}' limit 1");
		$hmd = $DB->get_row("SELECT * FROM meihe_hbmd WHERE domain='{$domain}' limit 1");

		if ($hmd && $hmd['type'] == 2) {
			exit('{"code":2,"msg":"当前域名为黑名单无法开通！"}');
			break;
		}

		if ($vipym2 && $vipym2['type'] == 2) {
			exit('{"code":2,"msg":"当前域名已被封禁,无法开通！"}');
			break;
		}

		if ($vipym2 && $vipym2['type'] == 1) {
			exit('{"code":2,"msg":"当前域名已存在无法重复开通！"}');
			break;
		}

		if (strexists($domain, '.')) {
			if (!preg_match('/^(http|ftp):\/\/[A-Za-z0-9]+\.[A-Za-z0-9]+[\/=\?%\-&_~`@[$$\’:+!]*([^<>\”])*$/', $domain)) {
				exit('{"code":2,"msg":"请您输入正确的域名！"}');
				break;
			}

			if (strlen($qq) > 12 || !is_numeric($qq) || strlen($qq) < 5 || !preg_match('/^[0-9]+$/', $qq)) {
				exit('{"code":2,"msg":"请输入正确的QQ号！"}');
				break;
			}

			$trade_no = date('YmdHis') . rand(111, 999);
			$input = $domain . '|' . $ktrq . '|' . $qq;

			if ($ktrq == 1) {
				$money = $conf['viphy'];
			} elseif ($ktrq == 2) {
				$money = $conf['viphy2'];
			} elseif ($ktrq == 3) {
				$money = $conf['viphy3'];
			} elseif ($ktrq == 4) {
				$money = $conf['viphy4'];
			} elseif ($ktrq == 5) {
				$money = $conf['viphy5'];
			}

			if ($money == 0) {
				$endtime = date('Y-m-d', strtotime('+ 1 months'));
				$sql = 'insert into `meihe_list` (`domain`,`date`,`type`,`endtime`,`qq`,`fhy`) values (\'' . $domain . '\',\'' . $date . '\',\'1\',\'' . $endtime . '\',\'' . $qq . '\',\'' . $conf['fhyyum'] . '\')';
				if (!$DB->query($sql)) {
					exit('{"code":-1,"msg":"开通VIP失败！' . $DB->{'error'}() . '"}');
					break;
				}
				exit('{"code":1,"msg":"恭喜您,开通会员成功！"}');
				break;
			}

			$sql = 'insert into `meihe_pay` (`trade_no`,`tid`,`zid`,`input`,`num`,`name`,`money`,`ip`,`userid`,`addtime`,`status`) values (\'' . $trade_no . '\',\'-3\',\'1\',\'' . $input . '\',\'1\',\'' . $conf['spmc'] . '\',\'' . $money . '\',\'' . $clientip . '\',\'' . $cookiesid . '\',\'' . $date . '\',\'0\')';
			if (!$DB->query($sql)) {
				exit('{"code":-1,"msg":"提交订单失败！' . $DB->{'error'}() . '"}');
				break;
			}
			$result = ['code' => 0, 'msg' => '提交订单成功！', 'trade_no' => $trade_no, 'money' => $money, 'pay_alipay' => $conf['alipay_api'], 'pay_wxpay' => $conf['wxpay_api'], 'pay_qqpay' => $conf['qqpay_api'], 'paymsg' => $conf['paymsg']];
			exit(json_encode($result));
			break;
		}
		exit('{"code":2,"msg":"域名无需输入http://格式"}');
		break;

	case 'bmdpay':
		$domain = daddslashes($_POST['domain']);
		$type = daddslashes($_POST['type']);
		$fwcsxx = daddslashes($_POST['fwcsxx']);
		$bmdgg = daddslashes($_POST['bmdgg']);
		$syskey = isset($_GET['key']) ? $_GET['key'] : $_POST['key'];

		if (!$syskey) {
			exit('{"code":-1,"msg":"接口密钥不能为空"}');
			break;
		}

		if ($syskey !== $conf['hh4_key']) {
			exit('{"code":-1,"msg":"接口密钥不正确"}');
			break;
		}

		if ($conf['bmdkt'] == 1) {
			exit('{"code":-1,"msg":"当前站点未开启自助开通白名单！' . $DB->{'error'}() . '"}');
			break;
		}

		if ($conf['captcha_open'] == 1 && $conf['ywgmyz'] == 1) {
			include_once ROOT . 'assets/css/class.geetestlib.php';
			$GtSdk = new GeetestLib($conf['captcha_id'], $conf['captcha_key']);
			$cookiesid = $_COOKIE['mysid'];
			$data = array('user_id' => $cookiesid, 'client_type' => 'web', 'ip_address' => real_ip());
			if (!$_POST['geetest_challenge'] && !$_POST['geetest_validate'] && !$_POST['geetest_seccode']) {
				exit('{"code":-1,"msg":"请先完成验证后再操作！"}');
				break;
			}
			if (!$GtSdk->{'success_validate'}($_POST['geetest_challenge'], $_POST['geetest_validate'], $_POST['geetest_seccode'], $data)) {
				exit('{"code":-1,"msg":"验证失败，如重复生成 请重新验证！"}');
				break;
			}
		}

		$vipym2 = $DB->get_row("SELECT * FROM meihe_list WHERE domain='{$domain}' limit 1");
		$hmd = $DB->get_row("SELECT * FROM meihe_hbmd WHERE domain='{$domain}' limit 1");

		if ($vipym2 && $vipym2['type'] == 1) {
			exit('{"code":2,"msg":"当前域名为会员无法开通！"}');
			break;
		}

		if ($hmd && $hmd['type'] == 3) {
			exit('{"code":2,"msg":"当前域名已存在无法重复开通！"}');
			break;
		}

		if (strexists($domain, '.')) {
			if (!preg_match('/^(http|ftp):\/\/[A-Za-z0-9]+\.[A-Za-z0-9]+[\/=\?%\-&_~`@[$$\’:+!]*([^<>\”])*$/', $domain)) {
				exit('{"code":2,"msg":"请您输入正确的域名！"}');
				break;
			}

			$trade_no = date('YmdHis') . rand(111, 999);
			$money = $conf['bmdjg'];
			$input = $domain . '|' . $type;

			$sql = 'insert into `meihe_pay` (`trade_no`,`tid`,`zid`,`input`,`num`,`name`,`money`,`ip`,`userid`,`addtime`,`status`) values (\'' . $trade_no . '\',\'-4\',\'1\',\'' . $input . '\',\'1\',\'' . $conf['spmc'] . '\',\'' . $money . '\',\'' . $clientip . '\',\'' . $cookiesid . '\',\'' . $date . '\',\'0\')';

			if (!$DB->query($sql)) {
				exit('{"code":-1,"msg":"开通白名单失败！' . $DB->{'error'}() . '"}');
				break;
			}
			$result = ['code' => 0, 'msg' => '提交订单成功！', 'trade_no' => $trade_no, 'money' => $money, 'pay_alipay' => $conf['alipay_api'], 'pay_wxpay' => $conf['wxpay_api'], 'pay_qqpay' => $conf['qqpay_api'], 'paymsg' => $conf['paymsg']];
			exit(json_encode($result));
			break;
		}

		exit('{"code":2,"msg":"域名无需输入http://格式"}');
		break;

	case 'klapi':
		$syskey = isset($_GET['key']) ? $_GET['key'] : $_POST['key'];

		if (!$syskey) {
			exit('{"code":-1,"msg":"克隆密钥不能为空"}');
			break;
		}

		if ($syskey !== $conf['hh_key']) {
			exit('{"code":-1,"msg":"克隆密钥错误"}');
			break;
		}

		$hh_kl = $DB->query('SELECT * FROM meihe_api WHERE 1');
		$data = array();
		foreach ($hh_kl as $res) {
			$data[] = array('sbm' => $res['sbm'], 'name' => $res['name'], 'apiurl' => $res['apiurl'], 'format' => $res['format'], 'fhzd' => $res['fhzd'], 'dljk' => $res['dljk'], 'duanl' => $res['duanl'], 'jktz' => $res['jktz']);
		}
		exit('{"code":1,"msg":"克隆执行成功","data":' . json_encode($data) . '}');
		break;

	case 'cxsj':
		$value = isset($_GET['longurl']) ? $_GET['longurl'] : $_POST['longurl'];

		if (empty($value)) {
			exit('{"msg":"URL不能为空"}');
			break;
		}

		if (strpos($value, 'http') === false) {
			$longurl = 'http://' . daddslashes($value);
		} else {
			$longurl = daddslashes($value);
		}

		$url_arr = parse_url($longurl);
		$domain = $url_arr['host'];

		$thtime = date('Y-m-d') . ' 00:00:00';
		$zgfw = $DB->count("SELECT count(*) FROM meihe_log WHERE domain='{$domain}' ");
		$jrfw = $DB->count("SELECT count(*) FROM meihe_log WHERE domain='{$domain}' and click_time>='{$thtime}'");
		$uvs = $DB->count("SELECT count(DISTINCT user_agent,ip_address) FROM meihe_log WHERE domain='{$domain}'");
		$ips = $DB->count("SELECT count(DISTINCT ip_address) FROM meihe_log WHERE domain='{$domain}'");

		$result = array('code' => 0, 'msg' => '查询域名：' . $domain . '<hr>今日访问：' . $jrfw . ' 次<hr>总共访问：' . $zgfw . ' 次<hr>UV数量：' . $uvs . ' 个<hr>IP数量：' . $ips . ' 个');
		exit(json_encode($result));
		break;

	case 'fangh':
		$uid = $_GET['i'];
		if ($uid == NULL) {
			$result = array('code' => -1, 'msg' => '请输入查识别码再执行！');
			exit(json_encode($result));
			break;
		}

		$row = $DB->get_row("select * from meihe_report where uid='{$uid}' limit 1");

		if (!$row) {
			$result = array('code' => -2, 'msg' => 'uid不存在无法获取访问！');
			exit(json_encode($result));
			break;
		}

		$t_url = $row['url'];

		$domain = parse_url($t_url, PHP_URL_HOST);

		$rowhhy = $DB->get_row('SELECT * FROM meihe_list WHERE domain=\'' . $domain . '\' limit 1');

		if (empty($rowhhy)) {
			$rowhhy = $DB->get_row('SELECT * FROM meihe_list WHERE domain=\'' . get_host($domain) . '\' and `fhy`=\'2\' limit 1');
		}

		if (empty($rowhhy) || !is_array($rowhhy)) {
			exit(json_encode(['code' => -3, 'msg' => '当前网址' . $t_url . '会员不存在！']));
		}

		$dateee = date('Y-m-d H:i:s');
		if ($rowhhy['endtime'] <= $dateee) {
			$result = array('code' => -3, 'msg' => '当前网址' . $t_url . '会员已到期！');
			exit(json_encode($result));
			break;
		}
		$duanl = trim($row['duanl']);
		$meihe_api = $DB->get_row("SELECT * FROM `meihe_api` WHERE `sbm` = '$duanl'");

		if (empty($meihe_api)) {
			exit(json_encode(['code' => -3, 'msg' => '当前网址' . $t_url . '接口不存在！']));
		}

		if ($meihe_api['jkfl'] != 2) {
			$yesterday_time = date('Y-m-d', strtotime('-1 month')) . ' 00:00:00';
			$meihepvy = $DB->count('SELECT count(*) FROM meihe_log where domain=\'' . $domain . '\' and `click_time` >=\'' . $yesterday_time . '\'');
			if ($meihepvy > $conf['month_pv']) {
				$result = array('code' => -3, 'msg' => '当前网址' . $t_url . '本月访问次数已上限！');
				exit(json_encode($result));
				break;
			}
			$thtime = date('Y-m-d', strtotime('-1 day')) . ' 00:00:00';
			$meihepvr = $DB->count('SELECT count(*) FROM meihe_log where domain=\'' . $domain . '\' and `click_time` >=\'' . $thtime . '\'');
			if ($meihepvr > $conf['day_pv']) {
				$result = array('code' => -3, 'msg' => '当前网址' . $t_url . '今日访问次数已上限！');
				exit(json_encode($result));
				break;
			}
		}

		$rowhy = $DB->get_row('SELECT * FROM meihe_list WHERE domain=\'' . $domain . '\' limit 1');
		if (empty($rowhy)) {
			$rowhy = $DB->get_row('SELECT * FROM meihe_list WHERE domain=\'' . get_host($domain) . '\' and `fhy`=\'2\' limit 1');
		}

		if ($rowhy['type'] == 1) {
			if ($conf['zdybt']) {
				$title = $conf['zdybt'];
			} else {
				$info = file_get_contents($t_url);
				preg_match('/<title>(.*?)<\/title>/', $info, $m);
				$title = $m[1] ?? ' ';
			}
			$result = array('code' => 'ok', 'bt' => $title, 'keywords' => $conf['keywords'], 'description' => $conf['description'], 'url' => $t_url);
			exit(json_encode($result));
			break;
		}

		$result = array('code' => -3, 'msg' => '当前网址' . $t_url . '会员已到期！');
		exit(json_encode($result));
		break;

	case 'dwzapi':
		$url = isset($_GET['url']) ? $_GET['url'] : $_POST['url'];

		if (!$url) exit('Url不能为空');

		if (!preg_match('/^http[s]?:\/\//', $url)) {
			$url = 'http://' . $url;
		}

		$myrow = $DB->get_row('select * from meihe_report where url=\'' . $url . '\' limit 1');

		if (!$myrow) {
			$uid = rand(7000, 65535);
			$DB->query('insert into `meihe_report` (`uid`,`url`) values (\'' . $uid . '\',\'' . $url . '\')');
			exit($conf['tbxy'] . $_SERVER['HTTP_HOST'] . '/api/s.php?' . $uid);
		}

		exit($conf['tbxy'] . $_SERVER['HTTP_HOST'] . '/api/s.php?' . $myrow['uid']);
		break;

	default:
		exit('{"code":-4,"msg":"易航系统提醒你: No Act"}');
		break;
}
